
//TODO:instanceof 就是查找原型链的过程

const Father = function () {
  this.bigName = '爸爸';
};
Father.prototype.sayHello = function () {
  console.log(`我是你${this.bigName}`);
}

// Child继承Father
const Child = function () {
  Father.call(this);
  this.smallName = '儿子';
}
Child.prototype = Object.create(Father.prototype);
Child.prototype.constructor = Child;

const child = new Child();
child.sayHello(); // 我是你爸爸

console.log(child instanceof Child); // true
console.log(child instanceof Father); // true
console.log(child instanceof Object); // true


// 手写 instanceOf
function instanceOf(a, b) {
  let proto = a.__proto__;
  const prototype = b.prototype;
  // 从当前 __proto__ 开始查找
  while (proto) {
    // 如果找到 null 还没有找到，返回 false
    if (proto === null) {
      return false;
    }
    // 如果 a.__proto__.xxx === b.prototype，返回 true
    if (proto === prototype) {
      return true;
    }
    // 进一步迭代
    proto = proto.__proto__;
  }
}

console.log('手写')
console.log(instanceOf(child, Child)); // true
console.log(instanceOf(child, Father)); // true
console.log(instanceOf(child, Object)); // true

